<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm pr-0">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealName">
            {{ form.dealName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否认缴比例出资')" prop="subscribed">
            <el-radio-group v-model="form.subscribed">
              <el-radio label="Y">{{ $t('是') }}</el-radio>
              <el-radio label="N">{{ $t('否') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('出资人列表') }}</span>
      </div>
      <el-table :data="form.fundLpRightsDetailList" border class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('出资人名称')" prop="lpName" align="center" min-width="160" />
        <el-table-column :label="$t('认缴金额') + $t('金额单位')" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ moneyFormat(scope.row.subscribedAmount, 'w') }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('认缴比例(%)')" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ moneyFormat(scope.row.subRatio) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('本项目占比%')" align="center" min-width="100px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('本项目占比%') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item
              :prop="'fundLpRightsDetailList.' + scope.$index + '.rights'"
              :rules="fundLpRightsDetailListRules.rights"
            >
              <Money v-model="scope.row.rights" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" min-width="60px">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="delHandler(scope.$index, scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addRows">{{ $t("添加") }}</el-button>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>

    <!-- 添加出资人 -->
    <el-dialog :title="$t('出资人列表')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <investorList ref="investorList" @getParam="getParam" />
    </el-dialog>
  </div>
</template>

<script>
import {
  saveFundLpRights,
  getFundLpRights,
  deleteDetail
} from '@/api/fund/lpRights/fundLpRights'
import investorList from './investorList.vue'
export default {
  components: {
    investorList
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      formOpen: false,
      // 表单校验
      rules: {},
      fundLpRightsDetailListRules: {
        rights: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  methods: {
    // 初始化数据
    init(fundId, dealId) {
      this.loading = true
      getFundLpRights(fundId, dealId).then((response) => {
        this.form = response.data
        this.calculation()
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: undefined,
        dealId: undefined,
        subscribed: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      if (this.$parent.$parent.rightsOpen !== undefined) {
        this.$parent.$parent.rightsOpen = false
      }
    },
    // 关闭form页面
    closeForm() {
      this.loading = false
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
      if (this.$parent.$parent.rightsOpen !== undefined) {
        this.$parent.$parent.rightsOpen = false
      }
    },
    calculation() {
      // 计算认缴比例
      if (
        this.form.fundLpRightsDetailList &&
        this.form.fundLpRightsDetailList.length > 0
      ) {
        var num = 0
        this.form.fundLpRightsDetailList.forEach((item) => {
          num = num + item.subscribedAmount
        })
        this.form.fundLpRightsDetailList.forEach((v) => {
          var scale = (v.subscribedAmount / num).toFixed(4) * 100
          this.$set(v, 'subRatio', scale)
        })
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          saveFundLpRights(this.form).then((response) => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    delHandler(index, row) {
      const id = row.id
      if (id !== undefined && id !== '' && id != null) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
          .then(function() {
            return deleteDetail(id)
          })
          .then(() => {
            this.form.fundLpRightsDetailList.splice(index, 1)
            this.calculation()
            return this.msgSuccess(this.$t('删除成功'))
          })
          .catch(function() {})
      } else {
        this.form.fundLpRightsDetailList.splice(index, 1)
        this.calculation()
      }
    },
    addRows() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.investorList.load(this.form.fundLpRightsDetailList)
      })
    },
    getParam(data) {
      console.log(data)
      if (data.length > 0) {
        if (this.form.fundLpRightsDetailList.length > 0) {
          this.form.fundLpRightsDetailList.map(row => {
             data.forEach((v, index) => {
                if (row.investorId === v.investorId) {
                  v = row
                  data.splice(index, 1)
                }
             })
          })
          console.log(data)
          data.forEach((v) => {
            this.form.fundLpRightsDetailList.push(v)
          })
          this.calculation()
        } else {
          console.log(3)
          this.form.fundLpRightsDetailList = data
        }
      }
    }
  }
}
</script>
